<?php
use \Gloudemans\Shoppingcart\Exceptions\ShoppingcartInvalidRowIDException;

class CartController extends BaseController {

    protected $layout = 'layouts.center';

	public function listItems()
	{
        try {
			$cart = Cart::content();
        } catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }
        $cartArr = array_values($cart->toArray());
        return $this->makeView('cart.index', $cartArr);
	}

	/**
	 * Adds a new product to the shopping cart.
	 *
	 * @return \Illuminate\Http\RedirectResponse
	 */
	public function add()
	{
		$posts = Input::all();

    	$rules = array(
    		'product_id' => 'required|exists:product,id',
    		'number' => 'required'
    	);
        $messages = array(
            'product_id.required' => '请提交产品',
            'product_id.exists' => '这个产品不存在',
            'number.required' => '请提交加入购物车的产品的数量'
        );

        $validator = Validator::make ( $posts, $rules, $messages );
        if ($validator->fails()) {
            // the given data did not pass validation
            return $this->makeView(
                'errors.400', 
                $validator->errors(),
                400
            );
        }

		$product_id = $posts['product_id'];
		$number = $posts['number'];

        try {
        	$product = Product::find($product_id);

			Cart::add(
				array(
					'id' => $product->id, 
					'name' => $product->name, 
					'qty' => $number, 
					'price' => $product->price, 
					'options'=> ['thumb' => $product->thumb]
				)
			);
        } catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }

        return $this->makeView('cart.add', [], 201);
	}

	/**
	 * update the quantity of one row of the cart
	 *
	 * @return \Illuminate\Http\RedirectResponse
	 */
	public function update()
	{
		$posts = Input::all();

    	$rules = array(
    		'rowid' => 'required',
    		'number' => 'required'
    	);
        $messages = array(
            'rowid.required' => '请提交要删除的行id',
            'number.required' => '请提交要更新的行的数字'
        );

        $validator = Validator::make ( $posts, $rules, $messages );
        if ($validator->fails()) {
            // the given data did not pass validation
            return $this->makeView(
                'errors.400', 
                $validator->errors(),
                400
            );
        }

		$rowid = Input::get('rowid');
		$number = Input::get('number');

        try {
			Cart::update($rowid, $number);
        } catch (ShoppingcartInvalidRowIDException $sciride) {
            return $this->makeView(
                'errors.400', 
                ['rowid' => ['这个行不存在']],
                400
            );

    	} catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }

		$item = Cart::get($rowid);
		$sub_total = $item->subtotal;
		$total = Cart::total();
		$count = Cart::count();

        return $this->makeView('cart.update', ['sub_total' => $sub_total, 'total' => $total, 'count' => $count]);
	}

	/**
	 * Adds a new product to the shopping cart.
	 *
	 * @return \Illuminate\Http\RedirectResponse
	 */
	public function remove()
	{
		$posts = Input::all();

    	$rules = array(
    		'rowid' => 'required',
    	);
        $messages = array(
            'rowid.required' => '请提交要删除的行id',
        );

        $validator = Validator::make ( $posts, $rules, $messages );
        if ($validator->fails()) {
            // the given data did not pass validation
            return $this->makeView(
                'errors.400', 
                $validator->errors(),
                400
            );
        }

        $rowid = $posts['rowid'];
        try {
			Cart::remove($rowid);
        } catch (ShoppingcartInvalidRowIDException $sciride) {
            return $this->makeView(
                'errors.400', 
                ['rowid' => ['这个行不存在']],
                400
            );

    	} catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }
		$total = Cart::total();
		$count = Cart::count();

        return $this->makeView('cart.remove', ['total' => $total, 'count' => $count]);
	}

	public function cartCount()
	{
        try {
			$count = Cart::count();
        } catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }

        return $this->makeView('cart.count', $count);
	}

	public function destroy()
	{
        try {
			Cart::destroy();
        } catch (Exception $ex) {
            Log::error($ex);
            return $this->makeView(
                'errors.503', 
                '咨询律师失败，请稍后再试。',
                503
            );
        }

        return $this->makeView('cart.destroy', []);
	}
}
